package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.repository.family.custom.LoanLargeDeliveryManagementRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.LoanLargeDeliveryManagementSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanLargeDeliveryManagementSearchResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanLargeDeliveryManagement.LOAN_LARGE_DELIVERY_MANAGEMENT;
@Repository
public class LoanLargeDeliveryManagementRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanLargeDeliveryManagementRepositoryCustom {
    @Override
    public Page<LoanLargeDeliveryManagementSearchResponse> list(LoanLargeDeliveryManagementSearchRequest searchRequest, Pageable pageable) {
        fields();
        eq(LOAN_LARGE_DELIVERY_MANAGEMENT.OWNNER,searchRequest.getOwnner());
        eq(LOAN_LARGE_DELIVERY_MANAGEMENT.DELIVERMETHOD,searchRequest.getDelivermethod());
        eq(LOAN_LARGE_DELIVERY_MANAGEMENT.AMOUNTINTERVAL,searchRequest.getAmountinterval());
        orderBy(LOAN_LARGE_DELIVERY_MANAGEMENT.SORT, Sort.Direction.ASC);
        return search(pageable,record->{
            LoanLargeDeliveryManagementSearchResponse loanLargeDeliveryManagementSearchResponse =
                    new LoanLargeDeliveryManagementSearchResponse();
            loanLargeDeliveryManagementSearchResponse.setId(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.ID));
            loanLargeDeliveryManagementSearchResponse.setAmountinterval(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.AMOUNTINTERVAL));
            loanLargeDeliveryManagementSearchResponse.setAuthor(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.CREATOR));
            loanLargeDeliveryManagementSearchResponse.setCreatedate(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.CREATEDATE));
            loanLargeDeliveryManagementSearchResponse.setCreatetime(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.CREATE_TIME));
            loanLargeDeliveryManagementSearchResponse.setType(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.TYPE));
            loanLargeDeliveryManagementSearchResponse.setDelivermethod(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.DELIVERMETHOD));
            loanLargeDeliveryManagementSearchResponse.setOperadate(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.OPERADATE));
            loanLargeDeliveryManagementSearchResponse.setOperatime(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.UPDATE_TIME));
            loanLargeDeliveryManagementSearchResponse.setOperator(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.UPDATOR));
            loanLargeDeliveryManagementSearchResponse.setOwnner(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.OWNNER));
            loanLargeDeliveryManagementSearchResponse.setSort(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.SORT));
            loanLargeDeliveryManagementSearchResponse.setMinamount(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.MINAMOUNT));
            loanLargeDeliveryManagementSearchResponse.setMaxamount(record.get(LOAN_LARGE_DELIVERY_MANAGEMENT.MAXAMOUNT));
            return loanLargeDeliveryManagementSearchResponse;
        });
    }

    @Override
    public Integer updateLoanLargeDeliveryManagement(LoanLargeDeliveryManagementSearchRequest request) {
        return dslContext.update(LOAN_LARGE_DELIVERY_MANAGEMENT)
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.OWNNER,request.getOwnner())
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.AMOUNTINTERVAL,request.getAmountinterval())
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.DELIVERMETHOD,request.getDelivermethod())
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.SORT,request.getSort())
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.TYPE,request.getType())
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.UPDATOR, CurrentUser.get().getName())
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.UPDATE_TIME, Timestamp.valueOf(DateUtils.getCurrentTime()))
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.OPERADATE, java.sql.Date.valueOf(DateUtils.getCurrentDate()))
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.MAXAMOUNT,request.getMaxamount())
                .set(LOAN_LARGE_DELIVERY_MANAGEMENT.MINAMOUNT,request.getMinamount())
                .where(LOAN_LARGE_DELIVERY_MANAGEMENT.ID.eq(request.getId()))
                .execute();
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_LARGE_DELIVERY_MANAGEMENT;
    }
}
